<div class="layui-card" style="height: 50px;line-height: 50px;">
    <div class="layui-breadcrumb" lay-filter="breadcrumb" style="visibility: visible;padding: 0 15px;">
        <a>系统管理</a><span lay-separator="">/</span>
        <a><cite>用户管理</cite></a>
    </div>
</div>
<div class="layui-fluid" style="margin: auto;" lay-title="用户管理">
    <div class="layui-col-md12">
        <div class="layui-card" style="position: relative;padding: 10px 15px;line-height: 24px;">
            <div class="layui-card-body nepadmin-table-full">
                <div class="layui-form toolbar">
                    <form class="layui-form" lay-filter="searchForm">
                        <div class="layui-form-item" style="margin-bottom: 5px;">
                            <div class="layui-inline">
                                <input id="key" name="key" class="layui-input" type="text" placeholder="用户/姓名">
                            </div>
                            <div class="layui-inline">
                                <a onclick="userList.search()" class="layui-btn icon-btn"><i class="layui-icon"></i>搜索</a>
                                <a onclick="userList.add()" class="layui-btn icon-btn"><i class="layui-icon"></i>添加</a>
                                <!--<button id="btnExp" class="layui-btn icon-btn"><i class="layui-icon"></i>导出</button>-->
                            </div>
                        </div>
                    </form>
                </div>
                <table id="userListId" lay-filter="userList"></table>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="TPL-list-table-status">
    {{#
    var status = {
    '0':{title:'禁用',color:'red'},
    '1':{title:'启用',color:'blue'}
    }[d.status];
    }}
    <span class="layui-badge layui-bg-{{status.color}}">{{ status.title }}</span>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-perms="sys:user:updateBatch" lay-event="updateBatch">重置</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit" lay-perms="sys:user:update">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-perms="sys:user:delete"  lay-event="del">删除</a>
</script>
<script>
    var userList = {};
    var UserList = function () {
        var admin = layui.admin,form = layui.form, $ =layui.jquery;
        //初始化列表
        this.init = function () {
            console.log(admin.routeParams())
            form.render();
            admin.table({
                elem: '#userListId',
                api: 'pageUserList',
                height: 'full-200',
                formFilter: 'searchForm',
                tableFilter:'userList',
                cols: [[
                    {title: '序号', type: 'numbers', fixed: true},
                    {title: '用户名', field: 'username'},
                    {title: '姓名', field: 'realName'},
                    {title: '登录IP', field: 'lastLoginIp'},
                    {title: '状态', templet: '#TPL-list-table-status', field: 'status'},
                    {
                        field: 'lastLoginTime',
                        sort: true,
                        title: '登录时间',
                        align: 'center',
                        templet:function (d) {
                            if(d.lastLoginTime){
                                return '<p><span title="{{d.lastLoginTime}}" class="nepadmin-c-gray">'+d.lastLoginTime+'</span></p>';
                            }else{
                                return "";
                            }
                        }
                    }
                    ,{title: '操作',fixed: 'right', width: 170, align:'center', toolbar: '#barDemo'}
                ]],
                tools:{
                    //修改
                    edit:function (row) {
                        admin.popup({
                            title:'修改用户',
                            url:'/sys/user/edit',
                            area: ['50%', '65%'], //宽高
                            data: row,
                            success:function (layer, index) {
                                var editUser = new EditUser(index);
                                editUser.init();
                            }
                        });
                    },
                    del:function (row) {
                        admin.confirm("是否删除？",function () {
                            var userIds = [];
                            userIds.push(row.id);
                            admin.post({
                                api:'deleteUser',
                                data:JSON.stringify(userIds),
                                contentType: 'application/json;charset=UTF-8',
                                success:function (res) {
                                    if(res.code == 0){
                                        admin.success('操作成功',function () {
                                            userList.search();
                                        });
                                    }
                                }
                            });
                        })
                    },
                    //重置密码
                    updateBatch:function (row) {
                        admin.confirm("是否重置密码为：“admin” ？",function () {
                            var requsetParmes = {
                                user:{
                                    password: "admin"
                                }
                            };
                            var userIds = [];
                            userIds.push(row.id);
                            requsetParmes.userIds = userIds;
                            admin.post({
                                api:'updateBatch',
                                data:JSON.stringify(requsetParmes),
                                contentType: 'application/json;charset=UTF-8',
                                success:function (res) {
                                    if(res.code == 0){
                                        admin.success('操作成功',function () {
                                            userList.search();
                                        });
                                    }
                                }
                            });
                        })
                    }
                }
            });
        };
        //新增
        this.add = function () {
            admin.popup({
                title:'新增用户',
                url:'/sys/user/add',
                area: ['50%', '65%'], //宽高
                success:function (layer,index) {
                   var addUser = new AddUser(index);
                    addUser.init();
                }
            });
        };
        //刷新表格当前页
        this.refresh = function () {
            admin.tableSearch('searchForm','userListId');
        };
        //条件搜索
        this.search = function () {
            admin.tableSearch('searchForm','userListId',true);
        };
        //重置搜索条件
        this.reset = function () {
            $("#key").val("");
        };
    };
    //引入组件
    /*layui.use(['admin', 'form', 'jquery'], function () {
        userList = new UserList();
        userList.init();
    });*/
    /**
     * 常用组件 加载左侧菜单时已引入layui对象
     * 组件文件名称 非组件对象名称
     * 已引入： admin、config、view、jquery、laytpl、element、form、layer、loadBar、dropdown、api、table、laypage、util
     * @type {UserList}
     */
    userList = new UserList();
    userList.init();
</script>